home *** CD-ROM | disk | FTP | other *** search
/ Pascal Super Library / Pascal Super Library (CW International)(1997).bin / TURB_VIS / MACROS / PICKS.PAS < prev   
Pascal/Delphi Source File  |  1992-01-31  |  6KB  |  223 lines

  1. unit Picks;
  2. (***************************************************************************
  3.                  Implements a generic PickList Object.
  4.             Copyright 1992 Cybersoft - All Rights Reserved.
  5.  ***************************************************************************)
  6.  
  7. (*
  8. Generic PickList.
  9.  
  10.   The PickList is Init'ed with values for a TREct that are used to compute
  11.   the other TRect values for the scroller, listbox, and buttons.
  12.  
  13. sample useage:
  14. type
  15.      { Used for Sorted list, maintaining record of unsorted position. }
  16.      TSortRecord = record
  17.        Name : String [50];
  18.        RecNum : integer;
  19.      end;
  20.  
  21.      POurList = ^TOurList;
  22.      TOurList = object (TSortedCollection)
  23.        function Compare (Key1, Key2 : Pointer): Integer; virtual;
  24.        procedure FreeItem (Item : Pointer); virtual;
  25.      end;
  26.  
  27.      POurListBox = ^TOurListBox;
  28.      TOurListBox = object (TSortedListBox)        {from StdDlg}
  29.        procedure HandleEvent (var Event : TEvent); virtual;
  30.        function GetText (Item : Integer; MaxLen : Integer) : String; virtual;
  31.      end;
  32.  
  33.  
  34.  
  35. function PickIt (var which : integer) : boolean;
  36. var
  37.     OurList           : POurList;
  38.     OurRecord         : TSortRecord;      { for records other than PStrings }
  39.     ListBox           : POurListBox;      { Optional }
  40.     OurScroller       : PView;            { Optional }
  41.     ItemNum           : Integer;
  42.  
  43. begin
  44.            { Must be done first -- sets value for the dialog's TRect }
  45.    New(OurPickList, Init(9,3,70,17));
  46.  
  47.            { Optional -- nil can be passed to ListItemPicked. }
  48.    OurScroller := New(PScrollbar, Init(ScrollBarTRect));
  49.            { Optional  -- nil can be passed to ListItemPicked. }
  50.    ListBox := New(POurListBox, Init(ListBoxTRect, 1, PScrollbar(OurScroller)));
  51.  
  52.  
  53.            { This is a procedure you write.            }
  54.            { Builds a PCollection or PSortedCollection.}
  55.    BuildSortedList (OurList);
  56.  
  57.    if OurPickList^.ListItemPicked(OurScroller, ListBox, OurList,
  58.                                   'Pick Something', ItemNum) then
  59.    begin
  60.      PickIt := true;
  61.      OurRecord := PSortRecord(OurList^.At(ItemNum))^;
  62.      which := OurRecord.RecNum;
  63.    end;
  64.  
  65.    Dispose (OurList, Done);
  66.    OurList := nil;
  67.    Dispose (OurPickList, Done);
  68. end;
  69. *)
  70.  
  71.  
  72.  
  73. interface
  74. uses Objects, Views, Dialogs, Drivers, App, MsgBox, StdDlg;
  75.  
  76. type
  77.   PPickDialog = ^TPickDialog;
  78.   TPickDialog = object(TDialog)
  79.     Focused : Integer;
  80.     ListBox : PListBox;
  81.     procedure HandleEvent(var Event : TEvent);virtual;
  82.   end;
  83.  
  84.  
  85.   PPickList = ^TPickList;
  86.   TPickList = object(TObject)
  87.     R             : TRect;
  88.     List          : PCollection;
  89.  
  90.     constructor Init (XA, YA, XB, YB : Integer);
  91.  
  92.     function ListItemPicked( OurScroller : PView;
  93.                              OurListBox  : PListBox;
  94.                              OurList     : PCollection;
  95.                              Title       : String;
  96.                              var ItemNum : Integer):boolean;
  97.   private
  98.     function CreatePickList( OurScroller : PView;
  99.                              OurListBox  : PListBox;
  100.                              OurList     : PCollection;
  101.                              Title       : string) : PPickDialog;
  102.  
  103.   end;
  104.  
  105.   PRect = ^TRect;
  106.  
  107.   var ScrollBarPRect,
  108.       ListBoxPRect : PRect;
  109.       
  110. implementation (***********************************************************)
  111.  
  112. {***************************** TPickDialog *********************************}
  113.  
  114. procedure TPickDialog.HandleEvent(var Event: TEvent);
  115. begin
  116.   Focused := ListBox^.Focused;
  117.   if ListBox^.MouseInView(MouseWhere) and
  118.            (Event.What = evMouseDown) and
  119.            (Event.Double = true) then
  120.     EndModal(cmOK);
  121.   TDialog.HandleEvent(Event);
  122. end;
  123.  
  124.  
  125.  
  126.  
  127. {************************** TPickList *************************************}
  128.  
  129. constructor TPickList.Init (XA, YA, XB, YB : Integer);
  130. begin
  131.   TObject.Init;
  132.   R.Assign (XA, YA, XB, YB);
  133.   ListBoxPRect^.Assign (3, 2, ((R.B.X - R.A.X) - 4), ((R.B.Y - R.A.Y) - 4));
  134.   ScrollBarPRect^.Assign (ListBoxPRect^.B.X, 2, ListBoxPRect^.B.X + 1,
  135.                          ListBoxPRect^.B.Y);
  136. end;
  137.  
  138.  
  139. function TPickList.CreatePickList( OurScroller : PView;
  140.                                    OurListBox  : PListBox;
  141.                                    OurList     : PCollection;
  142.                                    Title       : string) : PPickDialog;
  143. var
  144.   Dlg                   : PPickDialog;
  145.   Control, Labl,
  146.   Histry                : PView;
  147.   AScroller             : PView;
  148.   OurR                  : TRect;
  149.  
  150. Begin
  151. {R.Assign(6,3,73,21);}
  152. New(Dlg, Init(R, Title));
  153.  
  154. if OurScroller = nil then
  155. begin
  156.   AScroller := New(PScrollbar, Init(ScrollBarPRect^));
  157.   Dlg^.Insert(AScroller);
  158. end
  159. else
  160.   Dlg^.Insert(OurScroller);
  161.  
  162.  
  163. OurR.Assign(
  164.             ((R.B.X - R.A.X) - 26),
  165.             ((R.B.Y - R.A.Y) - 3),
  166.             ((R.B.X - R.A.X) - 16),
  167.             ((R.B.Y - R.A.Y) - 1)
  168.             );
  169. Control := New(PButton, Init(OurR, ' OK', cmOK, bfDefault));
  170. Dlg^.Insert(Control);
  171.  
  172. OurR.Assign(
  173.             ((R.B.X - R.A.X) - 13),
  174.             ((R.B.Y - R.A.Y) - 3),
  175.             ((R.B.X - R.A.X) - 3),
  176.             ((R.B.Y - R.A.Y) - 1)
  177.             );
  178. Control := New(PButton, Init(OurR, 'Cancel', cmCancel, bfNormal));
  179. Dlg^.Insert(Control);
  180.  
  181. if OurListBox = nil then
  182.   Dlg^.ListBox := New(PListBox, Init(ListBoxPRect^, 1, PScrollbar(OurScroller)))
  183. else
  184.   Dlg^.ListBox := OurListBox;
  185. Dlg^.ListBox^.NewList(OurList);
  186. Dlg^.Insert(Dlg^.ListBox);
  187.  
  188. CreatePickList := Dlg;
  189.  
  190. end;
  191.  
  192.  
  193.  
  194.  
  195. function TPickList.ListItemPicked(OurScroller : PView;
  196.                                   OurListBox  : PListBox;
  197.                                   OurList     : PCollection;
  198.                                   Title       : String;
  199.                                   var ItemNum : Integer):boolean;
  200. var D   : PPickDialog;
  201.     cmd : word;
  202. begin
  203.   ListItemPicked := false;
  204.   if OurList = nil then exit;
  205.   D := CreatePickList(OurScroller, OurListBox, OurList, Title);
  206.   cmd := Desktop^.ExecView(D);
  207.   if cmd = cmOK then begin
  208.     ListItemPicked := true;
  209.     ItemNum := D^.Focused;
  210.   end;
  211.   Dispose(D, Done);
  212. end;
  213.  
  214.  
  215.  
  216. {--------------------------------------------------------------------------}
  217.  
  218. begin
  219.   New (ListBoxPRect);
  220.   New (ScrollBarPRect);
  221.   ListBoxPRect^.Assign(0,0,0,0);
  222.   ScrollBarPRect^.Assign(0,0,0,0);
  223. end.